package com.jarvis.app.car.service;

import com.jarvis.app.car.common.MessageResult;
import com.jarvis.app.car.model.User;
import com.jarvis.app.car.util.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class UserService {

    public MessageResult<User> getOne(Long userId) {
        User user = null;

        Connection connection = null;
        PreparedStatement pst = null;
        ResultSet resultSet = null;

        try {
            String sql = "select * from user_info where id = ? ";

            connection = DBUtils.getConnection();
            pst = connection.prepareStatement(sql);
            pst.setLong(1, userId);

            resultSet = pst.executeQuery();


            while (resultSet.next()) {
                if (Objects.nonNull(user)) {
                    throw new RuntimeException("id 多条");
                }

                user = new User();
                long id = resultSet.getLong("id");
                String phoneResult = resultSet.getString("phone");
                String name = resultSet.getString("name");
                String pwd = resultSet.getString("pwd");
                int bolSuper = resultSet.getInt("bol_super");
                String img = resultSet.getString("img");

                user.setId(id);
                user.setName(name);
                user.setPhone(phoneResult);
                user.setBolSuper(bolSuper);
                user.setPwd(pwd);
                user.setImg(img);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                DBUtils.close(resultSet, pst, connection);
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }


        return MessageResult.ok(null, user);
    }

    public MessageResult<User> getOne(String phone, String pwd) {
        User user = null;

        Connection connection = null;
        PreparedStatement pst = null;
        ResultSet resultSet = null;

        try {
            String sql = "select * from user_info where phone = ? and pwd = ? ";

            connection = DBUtils.getConnection();
            pst = connection.prepareStatement(sql);
            pst.setString(1, phone);
            pst.setString(2, pwd);
            resultSet = pst.executeQuery();

            boolean judgeOne = false;
            while (resultSet.next()) {
                if (judgeOne) {
                    return MessageResult.fail("找到多条数据", null);
                }
                user = new User();
                long id = resultSet.getLong("id");
                String phoneResult = resultSet.getString("phone");
                String name = resultSet.getString("name");
                int bolSuper = resultSet.getInt("bol_super");
                String img = resultSet.getString("img");

                user.setId(id);
                user.setName(name);
                user.setPhone(phoneResult);
                user.setBolSuper(bolSuper);
                user.setImg(img);
                judgeOne = true;
            }
        } catch (SQLException e) {

            throw new RuntimeException(e);
        } finally {
            try {
                DBUtils.close(resultSet, pst, connection);
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }

        if (Objects.isNull(user)) {
            return MessageResult.fail("用户名密码不正确", null);
        }

        return MessageResult.ok(null, user);
    }

    public MessageResult<List<User>> listAll() {
        List<User> userList = new ArrayList<>();

        Connection connection = null;
        PreparedStatement pst = null;
        ResultSet resultSet = null;

        try {
            String sql = "select * from user_info ";

            connection = DBUtils.getConnection();
            pst = connection.prepareStatement(sql);
            resultSet = pst.executeQuery();


            while (resultSet.next()) {

                User user = new User();
                long id = resultSet.getLong("id");
                String phoneResult = resultSet.getString("phone");
                String name = resultSet.getString("name");
                int bolSuper = resultSet.getInt("bol_super");
                String img = resultSet.getString("img");

                user.setId(id);
                user.setName(name);
                user.setPhone(phoneResult);
                user.setBolSuper(bolSuper);
                user.setImg(img);

                userList.add(user);
            }
        } catch (SQLException e) {

            throw new RuntimeException(e);
        } finally {
            try {
                DBUtils.close(resultSet, pst, connection);
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }

        return MessageResult.ok(null, userList);
    }

    public MessageResult<Void> register(String name, String phone, String pwd, String img) {
        boolean exist = exist(phone);
        if (exist) {
            return MessageResult.fail("账号已存在");
        }
        Connection connection = null;
        PreparedStatement pst = null;
        ResultSet resultSet = null;

        try {
            String sql = "insert into user_info(name,phone,pwd,img) values(?,?,?,?) ";

            connection = DBUtils.getConnection();
            pst = connection.prepareStatement(sql);
            pst.setString(1, name);
            pst.setString(2, phone);
            pst.setString(3, pwd);
            pst.setString(4, img);

            int i = pst.executeUpdate();

            if (i > 0) {
                return MessageResult.ok();
            }
        } catch (SQLException e) {

            throw new RuntimeException(e);
        } finally {
            try {
                DBUtils.close(resultSet, pst, connection);
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }
        return MessageResult.fail();
    }

    public MessageResult<Void> updatePwd(Long userId, String pwd) {
        Connection connection = null;
        PreparedStatement pst = null;
        ResultSet resultSet = null;

        try {
            String sql = "update user_info set pwd = ? where id = ?";

            connection = DBUtils.getConnection();
            pst = connection.prepareStatement(sql);
            pst.setString(1, pwd);
            pst.setLong(2, userId);


            int i = pst.executeUpdate();

            if (i > 0) {
                return MessageResult.ok();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                DBUtils.close(resultSet, pst, connection);
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }
        return MessageResult.fail();
    }

    public MessageResult<Void> updateUserName(Long userId, String userName) {
        Connection connection = null;
        PreparedStatement pst = null;
        ResultSet resultSet = null;

        try {
            String sql = "update user_info set name = ? where id = ?";

            connection = DBUtils.getConnection();
            pst = connection.prepareStatement(sql);
            pst.setString(1, userName);
            pst.setLong(2, userId);


            int i = pst.executeUpdate();

            if (i > 0) {
                return MessageResult.ok();
            }
        } catch (SQLException e) {

            throw new RuntimeException(e);
        } finally {
            try {
                DBUtils.close(resultSet, pst, connection);
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }
        return MessageResult.fail();
    }

    public MessageResult<Void> delete(Long userId) {
        Connection connection = null;
        PreparedStatement pst = null;
        ResultSet resultSet = null;

        try {
            String sql = "delete from  user_info  where id = ?";

            connection = DBUtils.getConnection();
            pst = connection.prepareStatement(sql);

            pst.setLong(1, userId);
            int i = pst.executeUpdate();

            if (i > 0) {
                return MessageResult.ok();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                DBUtils.close(resultSet, pst, connection);
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }
        return MessageResult.fail();
    }

    private boolean exist(String phone) {
        boolean judge = false;

        Connection connection = null;
        PreparedStatement pst = null;
        ResultSet resultSet = null;

        try {
            String sql = "select count(*) from user_info where phone = ? ";

            connection = DBUtils.getConnection();
            pst = connection.prepareStatement(sql);
            pst.setString(1, phone);

            resultSet = pst.executeQuery();

            while (resultSet.next()) {

                judge = resultSet.getInt(1) > 0;
            }
        } catch (SQLException e) {

            throw new RuntimeException(e);
        } finally {
            try {
                DBUtils.close(resultSet, pst, connection);
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }


        return judge;
    }


    public MessageResult<Void> updateById(long userId, String userName, String phone, String password, String img) {
        Connection connection = null;
        PreparedStatement pst = null;
        ResultSet resultSet = null;

        try {
            String sql = "update user_info set name = ?,phone = ?,pwd = ?,img = ?  where id = ?";

            connection = DBUtils.getConnection();
            pst = connection.prepareStatement(sql);

            pst.setString(1, userName);
            pst.setString(2, phone);
            pst.setString(3, password);
            pst.setString(4, img);
            pst.setLong(5, userId);

            pst.executeUpdate();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                DBUtils.close(resultSet, pst, connection);
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }
        return MessageResult.ok();

    }
}
